package com.plupload;

import java.util.List;
import javax.faces.application.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 * This class is a session scoped Managed Bean resposible for the rendering of
 * plupload's script in component's page and also for the communication beetween
 * the component (plupload.xhtml) and the servlet (com.plupload.PluploadServlet).
 * 
 * @author Tiago Peres França
 * @version 0.1
 */
@ManagedBean(name = "Plupload")
@SessionScoped
public class Plupload {
    // list of uploaded temporary files.
    private List files;
    // path for the plupload's servlet.
    private String servletPath;
    // list of runtime preferences separated by comma.
    private String runtimePreferences;
    // maximum size per file (in bytes), 0 if there's no limit.
    private long maxFileSize;
    // width for image resizing. 0 if it's not relevent.
    private int resizingWidth;
    // height for image resizing. 0 if it's not relevent.
    private int resizingHeight;
    // quality for image resizing. 100 if it's not relevent.
    private int resizingQuality;
    // list of allowed file extensions separated by comma.
    private String allowedTypes;
    
    // script for plupload
    private String script;
    
    /***
     * Initialize the session with the attributes from plupload component.
     * 
     * @param files list of uploaded temporary files
     * @param servletPath list of runtime preferences separated by comma
     * @param runtimePreferences list of runtime preferences separated by comma
     * @param maxFileSize maximum size per file, 0 if there's no limit
     * @param chunkSize maximum size for each part of the file. 0 if no file partition is needed
     * @param resizingWidth width for image resizing. 0 if it's not relevent
     * @param resizingHeight height for image resizing. 0 if it's not relevent
     * @param resizingQuality quality for image resizing. 100 if it's not relevent
     * @param allowedTypes list of allowed file extensions separated by comma
     * 
     * @author Tiago Peres França
     * @version 0.1
     */
    public void inicialize(List files, String servletPath, String runtimePreferences, String maxFileSize, 
            String resizingWidth,String resizingHeight, String resizingQuality, String allowedTypes){
        this.files = files;
        this.runtimePreferences = runtimePreferences;
        this.maxFileSize = Long.parseLong(maxFileSize);
        this.resizingWidth = Integer.parseInt(resizingWidth);
        this.resizingHeight = Integer.parseInt(resizingHeight);
        this.resizingQuality = Integer.parseInt(resizingQuality);
        this.allowedTypes = allowedTypes;
        this.servletPath = servletPath;
        
        writeScript();
    }

    /**
     * @return the list of uploaded temporary files
     */
    public List getFiles() {
        return files;
    }
    
    /**
     * Generates the script for the plupload component's page
     * 
     * @author Tiago Peres França
     * @version 0.1
     */
    private void writeScript(){
        StringBuilder sb = new StringBuilder();
        sb.append("<script type=\"text/javascript\">$(function() {$(\"#uploaderB78EE5DCF63AA7412ECABB99\").pluploadQueue({runtimes : '");
        sb.append(runtimePreferences);
        sb.append("', url : '");
        sb.append(servletPath);
        sb.append("', ");
        if (maxFileSize > 0){
            sb.append("max_file_size : '");
            sb.append(maxFileSize);
            sb.append("', ");
        }
        sb.append("unique_names : false, ");
        if (resizingWidth > 0 || resizingHeight > 0 || resizingQuality < 100){
            sb.append("resize : {");
            if(resizingWidth > 0){
                sb.append("width : ");
                sb.append(resizingWidth);
                sb.append(", ");
            }
            if (resizingHeight > 0){
                sb.append("height : ");
                sb.append(resizingHeight);
                sb.append(", ");
            }
            sb.append("quality : ");
            sb.append(resizingQuality);
            sb.append("}, ");
        }
        if (!allowedTypes.equals("")){
            sb.append("filters : [{title : \"Allowed files\", extensions : \"");
            sb.append(allowedTypes);
            sb.append("\"}], ");
        }
        Resource flash = FacesContext.getCurrentInstance().getApplication().getResourceHandler().createResource("plupload.flash.swf", "plupload/js");
        Resource silverlight = FacesContext.getCurrentInstance().getApplication().getResourceHandler().createResource("plupload.silverlight.xap", "plupload/js");
        sb.append("flash_swf_url : '");
        sb.append(flash.getRequestPath());
        sb.append("', silverlight_xap_url : '");
        sb.append(silverlight.getRequestPath());
        sb.append("'");
        sb.append("});});</script>");
        
        script = sb.toString();
    }

    /**
     * @return the script for plupload component
     */
    public String getScript() {
        return script;
    }

    /**
     * @return the maximum size per file (in bytes)
     */
    public Long getMaxFileSize() {
        return maxFileSize;
    }
}
